
#include "mkdirDispose.h"


void mkdirDispose(int netfd,MYSQL * mysql,char * username,char * prev_path){
    printf("accept prev_path:%s\n",prev_path);    //fu dir
    char sql[1024] = {0};
    sprintf(sql,"select * from usrfile where username = '%s' and path = '%s';",username,prev_path);
    int qret = mysql_query(mysql,sql);
    if(qret != 0){
        fprintf(stderr,"mysql_query 1:%s\n",mysql_error(mysql));
        send(netfd,"0",1,MSG_NOSIGNAL); 
        return ;
    }
    MYSQL_RES * table =  mysql_store_result(mysql);
    if(mysql_num_rows(table) == 0){    //no fu  dir
            send(netfd,"0",1,MSG_NOSIGNAL);
            return;
    }
    MYSQL_ROW row1 = mysql_fetch_row(table);
    if(strcmp(row1[5],"f") == 0){
        send(netfd,"0",1,MSG_NOSIGNAL);
        return ; 
    }

    send(netfd,"1",1,MSG_NOSIGNAL);
    
    
    int epollfd = epoll_create(1);
    struct epoll_event event;
    event.events = EPOLLIN;
    event.data.fd = netfd;
    epoll_ctl(epollfd,EPOLL_CTL_ADD,netfd,&event);
    struct epoll_event readyset[5];
    epoll_wait(epollfd,readyset,5,-1);

    Train train;

    char filename[1024] = {0};
    recv(netfd,&train.length,sizeof(train.length),MSG_WAITALL);
    recv(netfd,train.data,train.length,MSG_WAITALL);
    memcpy(filename,train.data,train.length);
    

    char filepath[1024] = {0};
    recv(netfd,&train.length,sizeof(train.length),MSG_WAITALL);
    recv(netfd,train.data,train.length,MSG_WAITALL);
    memcpy(filepath,train.data,train.length);
    
    bzero(sql,sizeof(sql));
    sprintf(sql,"insert into usrfile (filename,username,parent_id,path,type) values ('%s','%s',%s,'%s','%c') ;",filename,username,row1[0],filepath,'d');
    qret = mysql_query(mysql,sql);
    if(qret != 0){
        fprintf(stderr,"mysql_query 2:%s\n",mysql_error(mysql));
        return ;
    }
 
    return ;
}



